// hyb_yl/krecovery/pages/choose_operation/choose_operation.js
var app = getApp();
var o = function (o) {
  return o && o.__esModule ? o : {
    default: o
  };
}(require("../../../../utils/toot.js"));
var bmap = require('../../../../utils/bmap-wx.min.js');
var tcity = require("../../../../utils/citys.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    patient_state: false,
    sex_text: "",
    shuqianImgList: [],
    shuhouImgList: [],
    showCondition: false,
    title: '1111',
    height: app.globalData.height,
    zid: "",
    zongheList: [{
      items: "综合排序",
      checked: false
    },
    {
      items: "回答次数",
      checked: false
    },
    {
      items: "星级评分",
      checked: false
    },
    {
      items: "响应时间",
      checked: false
    },

    ],
    tabList: ['选择签约医生', '选择手术项目', '填写描述/选择主诉', '上传术前术后报告', '选择患者'],
    conditionList: [{
      name: '全部',
      state: false
    },
    {
      name: '湿疹',
      state: false
    },
    ],
    tabIndex: 0,
    keshiList: [{
      keshititle: '常见科室',
      list: [{
        img: "",
        title: "皮肤科"
      }]
    }],
    operationList: [{
      operationtitle: '手术项目',
      list: [{
        state: false,
        title: '阑尾手术'
      }, {
        state: false,
        title: '阑尾手术'
      }, {
        state: false,
        title: '阑尾手术'
      }, {
        state: false,
        title: '阑尾手术'
      }]
    }],
    zongheText: '综合排序',
    keshi: "",
    ks_title: "",
    green: "",
    z_id: "",
    titles: "",
    keyword: "",
    content: "",
    label: [],
    page: 0,
    list: [],
    z_zhicheng: "",
    city: "全国",
    tabdex: "",
    order: "",
    green_money: "",
    time_length: "",
    j_id: "",
    numcard: "",
    sex: "",
    names: "",
    tel: "",
    region: "",
    green_title: "",
    green_thumb: "",
    agentname: "",
    hid: ""
  },
  tabbtn(e) {
    if (e.currentTarget.dataset.dex === this.data.tabdex) {
      this.setData({
        popshow: false,
        tabdex: "",
      })
    } else {
      this.setData({
        popshow: true,
        tabdex: e.currentTarget.dataset.dex,

      })
    }
  },
  // 关键字输入
  changetitle: function (e) {
    this.setData({
      titles: e.detail.value,
      page: 0,
      list: []
    })
    this.getzhuanjia();
  },
  // 选择专家
  tocontent: function (e) {
    var that = this;
    var zid = e.currentTarget.dataset.zid;
    var z_name = e.currentTarget.dataset.z_name;
    var isqianyue = e.currentTarget.dataset.isqianyue;
    var j_id = e.currentTarget.dataset.j_id;
    var keshi = e.currentTarget.dataset.keshi;
    var ks_title = e.currentTarget.dataset.ks_title;
    app.util.request({
      'url':"entry/wxapp/green.check_isbuy",
      data:{
        openid:wx.getStorageSync('openid'),
        zid:zid,
      },
      success:function(res)
      {
        var data = res.data;
        if(data.code == '0')
        {
          that.setData({
            zid: zid,
            z_name: z_name,
            tabIndex: 1,
            agentname: e.currentTarget.dataset.agentname,
            hid: e.currentTarget.dataset.hid,
            keshi: keshi,
            ks_title: ks_title,
          })
          that.getsh_type()
          that.getLabel()
          
        }else if(data.code == '1' || data.code == '2')
        {
          wx.showModal({
            title: '提示',
            content:data.message,
            showCancel:false,
            success:function(con)
            {
              if(con.confirm)
              {
                wx.navigateBack({})
              }
            }
          })
          
        }else if(data.code == '3')
        {
          that.setData({
            zid: zid,
            z_name: z_name,
            tabIndex: 1,
            agentname: e.currentTarget.dataset.agentname,
            hid: e.currentTarget.dataset.hid,
            keshi: keshi,
            ks_title: ks_title,
          })
          that.getsh_type()
          that.getLabel()
        }else if(data.code == '4')
        {
          wx.navigateTo({
            url: '/hyb_yl/krecovery/pages/order_list/order_list',
          })
        }
      }
    })
    

  },
  getsh_type: function () {
    var that = this;
    app.util.request({
      'url': "entry/wxapp/green.getsh_type",
      data: {
        zid: that.data.zid
      },
      success: function (res) {
        that.setData({
          green_list: res.data
        })
      }
    })
  },
  itemchecked: function (e) {
    var order = e.currentTarget.dataset.dex;
    this.setData({
      order: order,
      page: 0,
      list: [],
      zongheText: e.currentTarget.dataset.text,
      popshow: false
    })
    this.getzhuanjia();
  },
  // 省份选择
  provinceitem(e) {
    var val = e.currentTarget.dataset.dex
    var t = this.data.provincesdex;
    var cityData = this.data.cityData;
    if (val != t) {
      if (val > 0) {
        let ci = val - 1
        let citys = [];
        for (let i = 0; i < cityData[ci].sub.length; i++) {
          citys.push(cityData[ci].sub[i].name)
        }
        this.setData({
          citys,
          provincesdex: val
        })
      } else if (val == 0) {
        let citys = ["全国", "北京", "上海", "广州", "杭州", "西安", "武汉"];
        this.setData({
          citys,
          provincesdex: val
        })
      }
    }
  },
  // 城市选择
  cityitem(e) {
    var val = e.currentTarget.dataset.dex
    var name = e.currentTarget.dataset.name
    this.setData({
      popshow: false,
      page: 0,
      city: name,
      page: 0,
      list: []
    })
    this.getzhuanjia()
  },
  // 职称选择
  typesclick1(e) {
    var that = this
    let id = e.currentTarget.dataset.id

    this.setData({
      z_zhicheng: id
    })
  },
  // 重置
  resultclick() {
    this.setData({
      z_zhicheng: "",
      city: "",
      titles: '',
      order: '',
      page: 0,
      popshow: false,
      list: []
    })
    this.getzhuanjia();
  },
  // 完成
  submitclick() {
    var that = this
    var z_zhicheng = that.data.z_zhicheng
    that.setData({
      page: 0,
      list: [],
      popshow: false,
      z_zhicheng: z_zhicheng
    })
    that.getzhuanjia()
  },
  choose_tab: function (e) {
    var index = e.currentTarget.dataset.index;
    var that = this;
    var keshi = that.data.keshi;
    var ks_title = that.data.ks_title;
    var green = that.data.green;
    var zid = that.data.zid;

    if (index != 0 && (keshi == '' || ks_title == '')) {
      wx.showToast({
        title: '请先选择科室',
      })
    } else if ((index == 2 || index == 3 || index == 4 || index == 5) && green == '') {
      wx.showToast({
        title: '请选选择项目',
      })
    } else if ((index == 3 || index == 4 || index == 5) && zid == '') {
      wx.showToast({
        title: '请选择医生',
      })
    } else if ((index == 4 || index == 5) && that.data.content == '') {
      wx.showToast({
        title: '请填写描述',
      })
    } else if (index == 5 && that.data.shuhouImgList.length == 0) {
      wx.showToast({
        title: '请上传照片',
      })
    } else {
      that.setData({
        tabIndex: index
      })
    }

  },
  // 返回上一步、如果是第一步返回上一页
  _navback: function (e) {
    var tabIndex = this.data.tabIndex;
    console.log(tabIndex)
    if (tabIndex > 0) {
      this.setData({
        tabIndex: tabIndex - 1
      })
    } else if (tabIndex == 0) {
      wx.navigateBack({
        delta: 1,
      })
    }
  },

  choose_condition: function (e) {
    var label_arr = this.data.label_arr;
    var index = e.currentTarget.dataset.index;
    label_arr[index].state = !label_arr[index].state
    this.setData({
      label_arr
    })
  },
  // 标签选择
  choose_condition1: function (e) {
    var label_arr = this.data.label_arr;
    var index = e.currentTarget.dataset.index;
    label_arr[index].state = !label_arr[index].state
    this.setData({
      label_arr
    })
  },
  // 输入描述
  changecontent: function (e) {
    var that = this;
    that.setData({
      content: e.detail.value
    })
  },
  // 上传术前后报告

  toupload: function (res) {
    var that = this;
    var content = that.data.content;
    if (content == '') {
      wx.showToast({
        title: '请先填写病情描述',
      })
    } else {
      that.setData({
        tabIndex: 3
      })
    }
  },
  show_condition: function () {
    this.setData({
      showCondition: !this.data.showCondition
    })
  },
  // 返回首页
  _backhome: function (e) {
    wx.reLaunch({
      url: '/hyb_yl/tabBar/index/index',
    })
  },
  choose_operation: function (e) {
    var id = e.currentTarget.dataset.id;
    this.setData({
      green: id,
      tabIndex: 2,
      green_title: e.currentTarget.dataset.title,
      green_thumb: e.currentTarget.dataset.thumb,
      green_money: e.currentTarget.dataset.money,
      time_length: e.currentTarget.dataset.time_length
    })
  },
  // 上传术前照片
  add_img: function (e) {
    var that = this;
    var shuqianImgList = that.data.shuqianImgList;
    var uniacid = app.siteInfo.uniacid
    wx.chooseImage({
      count: 5 - that.data.shuqianImgList.length,
      success: function (res) {
        const tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths)
        for (var i = 0; i < res.tempFilePaths.length; i++) {
          wx.uploadFile({
            url: that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=Uploadback&m=hyb_yl',
            filePath: tempFilePaths[i],
            name: 'upfile',
            formData: [],
            success: function (res) {
              shuqianImgList.push(res.data)
              that.setData({
                shuqianImgList: shuqianImgList,
              })
            }
          })
        }
      }
    })
  },
  remove_img: function (e) {
    var index = e.currentTarget.dataset.index;
    var shuqianImgList = this.data.shuqianImgList;
    shuqianImgList.splice(index, 1);
    this.setData({
      shuqianImgList
    })
  },
  // 上传术后照片
  add_imgs: function (e) {
    var that = this;
    var shuhouImgList = that.data.shuhouImgList;
    var uniacid = app.siteInfo.uniacid
    wx.chooseImage({
      count: 5 - that.data.shuhouImgList.length,
      success: function (res) {
        const tempFilePaths = res.tempFilePaths
        for (var i = 0; i < res.tempFilePaths.length; i++) {
          wx.uploadFile({
            url: that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=Uploadback&m=hyb_yl',
            filePath: tempFilePaths[i],
            name: 'upfile',
            formData: [],
            success: function (res) {
              shuhouImgList.push(res.data)
              that.setData({
                shuhouImgList: shuhouImgList,
              })
            }
          })
        }
      }
    })
  },
  remove_imgs: function (e) {
    var index = e.currentTarget.dataset.index;
    var shuhouImgList = this.data.shuhouImgList;
    shuhouImgList.splice(index, 1);
    this.setData({
      shuhouImgList
    })
  },
  // 上传完照片选择患者
  chooseuser: function () {
    var that = this;
    var shuqianImgList = that.data.shuqianImgList;
    var shuhouImgList = that.data.shuhouImgList;
    if (shuqianImgList.length <= 0 || shuhouImgList.length <= 0) {
      wx.showToast({
        title: '请上传报告',
      })
    } else {
      that.setData({
        tabIndex: 4
      })
      that.getMyalluser();
    }
  },
  getMyalluser() {
    var that = this
    //检查是否存在一条自己的信息
    app.util.request({
      'url': 'entry/wxapp/user.alluserfamily',
      data: {
        openid: wx.getStorageSync("openid")
      },
      success: function (res) {
        console.log(res)
        if (res.data.length > 0) {
          that.setData({
            archivesArr: res.data,
            j_id: res.data[0].j_id,
            patient_index: res.data[0].index,
            sick_index: res.data[0].sick_index,
            numcard: res.data[0].numcard,
            sex: res.data[0].sex,
            names: res.data[0].names,
            j_id: res.data[0].j_id,
            tel: res.data[0].tel,
            region: res.data[0].region,
            age: res.data[0].age
          })
        }

      },
      fail: function (err) {
        //(err)
      },
    })
  },
  addPeo(e) {
    var sickrela = JSON.stringify(this.data.sickrela)
    var sick_index = e.currentTarget.dataset.sick_index
    console.log(sick_index)
    wx.navigateTo({
      url: "/hyb_yl/userCommunicate/pages/preindex/sickinfo/index?sickrela=" + sickrela + '&sick_index=' + sick_index + '&last=' + this.data.last
    })
  },
  choose_patient: function (e) {
    var index = e.currentTarget.dataset.index;
    var sick_index = e.currentTarget.dataset.sick_index
    var numcard = e.currentTarget.dataset.numcard
    var sex = e.currentTarget.dataset.sex
    var names = e.currentTarget.dataset.names;
    var j_id = e.currentTarget.dataset.j_id;
    var tel = e.currentTarget.dataset.tel;
    var region = e.currentTarget.dataset.region;
    this.setData({
      patient_index: index,
      sick_index: sick_index,
      numcard: numcard,
      sex: sex,
      names: names,
      j_id: j_id,
      tel: tel,
      region: region,
      age: e.currentTarget.dataset.age
    })
  },
  add_patient: function (e) {
    this.setData({
      patient_state: true
    })
  },
  close_zhe: function () {
    this.setData({
      patient_state: false
    })
  },
  sex_choose: function (e) {
    var sex = e.currentTarget.dataset.sex
    this.setData({
      sex: sex
    })
  },
  submit: function (res) {
    console.log(res)
    var that = this
    var value = res.detail.value;
    value.openid = wx.getStorageInfoSync("openid");
    value.sick_index = 1;
    if (value.name == '') {
      wx.showToast({
        icon: 'none',
        title: '请输入姓名',
      })
    } else if (value.tel == '') {
      wx.showToast({
        icon: 'none',
        title: '请输入手机号',
      })
    } else if (this.data.sex == '') {
      wx.showToast({
        icon: 'none',
        title: '请选择性别',
      })
    } else if (value.tizhong == '') {
      wx.showToast({
        icon: 'none',
        title: '请输入体重',
      })
    }
    else if (value.age == '') {
      wx.showToast({
        icon: 'none',
        title: '请输入年龄',
      })
    } else if (value.agreement.length == 0) {
      wx.showToast({
        title: '请阅读并同意协议',
        icon: 'none',
      })
    } else {
      app.util.request({
        'url': "entry/wxapp/green.addjiaren",
        data: value,
        success: function (res) {
          that.setData({
            patient_state: false
          })
          that.getMyalluser();
        }
      })

    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var id = options.id;
    var title = options.title;
    var keyword = options.keyword;
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    that.setData({
      bgc: bgc
    })
    that.setData({
      id: id,
      title: title,
      keyword: keyword
    })
    wx.setNavigationBarTitle({
      title: title,
    })
    var tabIndex = options.tabIndex;
    if (tabIndex) {
      that.setData({
        tabIndex: tabIndex
      })
    }
    app.util.request({
      'url': 'entry/wxapp/hzbingli.url',
      success: function (res) {
        console.log(res)
        that.setData({
          url: res.data,
        })
      }
    })
    var zid = options.zid;
    if (zid != undefined && zid != 'undefined') {
      that.setData({
        zid: zid,
        z_name: options.z_name,
        tabIndex: 1,
        agentname: options.agentname,
        hid: options.hid,
        keshi: options.keshi,
        ks_title: options.ks_title,
      })
      that.getsh_type()
      that.getLabel()

    }
    that.getAlldepartment();
    that.getzhuanjia();
    that.getAlldoczhicheng()
    that.getHotcity();
  },

  getHotcity: function () {
    var that = this;
    app.util.request({
      'url': "entry/wxapp/zhuanjia.hot_address",
      success: function (res) {
        var citys = [];
        for (var i = 0; i < res.data.length; i++) {
          citys.push(res.data[i].name)
        }
        tcity.init(that);
        var cityData = that.data.cityData;
        const provinces = [];
        provinces.push("热门城市");
        for (let i = 0; i < cityData.length; i++) {
          provinces.push(cityData[i].name);
        }

        that.setData({
          provinces,
          citys,
        })
      }
    })
  },
  getAlldoczhicheng() {
    var that = this
    app.util.request({
      'url': 'entry/wxapp/zhuanjia.getzhichenglist',
      success: function (res) {
        console.log(res)
        that.setData({
          section2: res.data
        })
      }
    })
  },
  getzhuanjia: function (res) {
    var that = this;
    app.util.request({
      'url': "entry/wxapp/green.zhuanjia_list",
      data: {
        title: that.data.titles,
        keyword: that.data.keyword,
        city: that.data.city,
        page: that.data.page,
        order: that.data.order,
        z_zhicheng: that.data.z_zhicheng,
        zid: that.data.zid,
        openid: wx.getStorageSync('openid')
      },
      success: function (res) {
        that.setData({
          page: that.data.page + 1,
          list: that.data.list.concat(res.data)
        })
      }
    })
  },
  // 获取科室
  getAlldepartment() {
    var that = this
    app.util.request({
      'url': 'entry/wxapp/faxian.departments',
      data: {},
      success: function (res) {
        console.log(res)
        var officelist = res.data
        that.setData({
          officelist: officelist,
          isLoading: !1
        })
      }
    })
  },

  handleTap: function (e) {
    var keshi = e.currentTarget.dataset.id;
    var title = e.currentTarget.dataset.tit;
    this.setData({
      keshi: keshi,
      ks_title: title,
      tabIndex: 1,
    })
    this.getLabel();
  },
  // 获取标签
  getLabel: function () {
    var that = this;
    app.util.request({
      'url': "entry/wxapp/green.label_list",
      data: {
        id: that.data.keshi
      },
      success: function (res) {
        that.setData({
          label_arr: res.data
        })
      }
    })
  },
  toorder: function (res) {
    var that = this;
    var keshi = that.data.keshi;
    var green = that.data.green;
    var green_money = that.data.green_money;
    var time_length = that.data.time_length;
    var zid = that.data.zid;
    var keyword = that.data.keyword;
    var laber_arr = that.data.label_arr;
    var content = that.data.content;
    var shuqianImgList = that.data.shuqianImgList;
    var shuhouImgList = that.data.shuhouImgList;
    var j_id = that.data.j_id;
    var ks_title = that.data.ks_title;
    if (keshi == '') {
      wx.showToast({
        title: '请选择科室',
      })
    } else if (green == '') {
      wx.showToast({
        title: '请选择项目',
      })
    } else if (zid == '') {
      wx.showToast({
        title: '请选择医生',
      })
    } else if (content == '') {
      wx.showToast({
        title: '请填写描述',
      })
    } else if (shuqianImgList.length == 0) {
      wx.showToast({
        title: '请上传报告',
      })
    } else if (j_id == '') {
      wx.showToast({
        title: '请选择患者',
      })
    } else {
      var newArry = []
      for (var i = 0; i < laber_arr.length; i++) {
        if (laber_arr[i].state) {
          newArry.push(laber_arr[i].name)
        }
      }
      var biaoqian = JSON.stringify(newArry);
      var shuqianImgLists = JSON.stringify(shuqianImgList);
      var shuhouImgLists = JSON.stringify(shuhouImgList);
      wx.navigateTo({
        url: '/hyb_yl/krecovery/pages/postoperative_order/postoperative_order?keshi_two=' + keshi + '&typeid=' + green + "&zid=" + zid + "&content=" + content + "&j_id=" + j_id + '&shuqianimg=' + shuqianImgLists + "&shuhouimg=" + shuhouImgLists + '&biaoqian=' + biaoqian + '&green_money=' + green_money + '&time_length=' + time_length + '&keyword=' + keyword + '&numcard=' + that.data.numcard + '&sex=' + that.data.sex + '&names=' + that.data.names + '&tel=' + that.data.tel + '&region=' + that.data.region + '&ks_title=' + ks_title + '&title=' + that.data.title + "&agentname=" + that.data.agentname + "&hid=" + that.data.hid + '&green_title=' + that.data.green_title + '&green_thumb=' + that.data.green_thumb + '&age=' + that.data.age,
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})